﻿using GEPL.Extensions;
using System;
using System.Windows.Controls;

namespace GEPL.Projects
{
    /// <summary>
    /// Represents a text output for ConsoleProject
    /// </summary>
    public class ConsoleOutput
    {
        /// <summary>
        /// TextBox control used as program output
        /// </summary>
        public TextBox TextBox { get; private set; }

        /// <summary>
        /// Creates an instance of class
        /// </summary>
        /// <param name="textBox">TextBox control used as program output</param>
        public ConsoleOutput(TextBox textBox)
        {
            TextBox = textBox;
        }

        /// <summary>
        /// Appends given strings to the TextBox
        /// </summary>
        /// <param name="str">Strings to write out</param>
        public void Write(params string[] str)
        {
            TextBox.InvokeWithParam(WriteAction, str);
        }

        /// <summary>
        /// Appends given strings to the TextBox.
        /// Every string is written in separate line.
        /// </summary>
        /// <param name="str">Strings to write out</param>
        public void WriteLine(params string[] str)
        {
            TextBox.InvokeWithParam(WriteLineAction, str);
        }

        /// <summary>
        /// Clears TextBox.
        /// </summary>
        public void Clear()
        {
            TextBox.Invoke(() => { TextBox.Text = String.Empty; });
        }

        private void WriteAction(object param)
        {
            foreach (var str in (string[])param)
            {
                TextBox.AppendText(str);
            }
            TextBox.ScrollToEnd();
        }

        private void WriteLineAction(object param)
        {
            foreach (var str in (string[])param)
            {
                TextBox.AppendText(str + "\n");
            }
            TextBox.ScrollToEnd();
        }
    }
}